package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.mapper.Sms_Seckill_Session_Mapper;
import com.shop.mapper.Sms_Seckill_Sku_Relation_Mapper;
import com.shop.pojo.sms.Sms_Seckill_Session;
import com.shop.pojo.sms.Sms_Seckill_Sku_Relation;
import com.shop.service.Sms_Seckill_Session_Service;
import com.shop.to.SeckillSkuRelationAndSessionTo;
import com.shop.utils.LayuiUtils;
import com.shop.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

import javax.management.QueryEval;
import java.util.Date;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 秒杀活动场次
 */
@Service
public class Sms_Seckill_Session_ServiceImpl implements Sms_Seckill_Session_Service {

    @Autowired
    private Sms_Seckill_Session_Mapper sms_seckill_session_mapper;
    @Autowired
    private Sms_Seckill_Sku_Relation_Mapper sms_seckill_sku_relation_mapper;

    /**
     * 后台的秒杀活动场次显示所有
     * @param pages
     * @param sss
     * @return
     */
    @Override
    public LayuiUtils<Sms_Seckill_Session> find_admin_seckill_session_All(Page<Sms_Seckill_Session> pages, Sms_Seckill_Session sss) {
        LayuiUtils<Sms_Seckill_Session>  layuis = new LayuiUtils<>();

        QueryWrapper<Sms_Seckill_Session> query = new QueryWrapper<>();

        if(sss.getName() != null){
            query.like("name",sss.getName());
        }

        Page<Sms_Seckill_Session> sms_seckill_sessionPage = sms_seckill_session_mapper.selectPage(pages, query);

        layuis.setCount(sms_seckill_sessionPage.getTotal());
        layuis.setData(sms_seckill_sessionPage.getRecords());


        return layuis;
    }

    /**
     * 后台的秒杀活动场次新增
     * @param sss
     * @return
     */
    @Override
    public R insert_admin_seckill_session_All(Sms_Seckill_Session sss) {

        int insert = sms_seckill_session_mapper.insert(sss);
        if (insert > 0){
            return R.success();
        }

        return R.error(500,"添加失败");
    }

    /**
     * 后台的秒杀活动场次删除
     * @param id
     * @return
     */
    @Override
    public R del_admin_seckill_session_All(List<Integer> id) {

        int i = sms_seckill_session_mapper.deleteBatchIds(id);

        if(i > 0){
            return R.success();
        }

        return R.error(500,"删除失败");
    }

    /**
     * 后台的秒杀活动场次修改
     * @param sss
     * @return
     */
    @Override
    public R up_admin_seckill_session_All(Sms_Seckill_Session sss) {

        int i = sms_seckill_session_mapper.updateById(sss);

        if(i > 0){
            return R.success();
        }

        return R.error(500,"修改失败");
    }

    /*
     * @Description 查询最近三天的活动场次和下面的商品
     * 1、首先根据当天的0点和距离当天还有3天时间的23点59.59进行查询场次
     * 2、copy当前查询的全部数据到to类里
     * 3、然后要根据当前活动场次id来查询当前场次下的全部商品（sku）
     * 4、最后返回一个to模型的查询，该to包含所有的场次和每个场次下的全部sku商品
     * @param
     * @return com.shop.utils.R
     * @author liusupeng
     * @date 2020/11/15 19:58
     */
    @Override
    public R findAllSeckill3DaysSession() {
QueryWrapper<Sms_Seckill_Session> queryWrapperSession=new QueryWrapper<>();
        queryWrapperSession.between("start_time",startDays(),endDays());
        List<Sms_Seckill_Session> sms_seckill_sessions = sms_seckill_session_mapper.selectList(queryWrapperSession);
       System.out.println(sms_seckill_sessions);
        List<SeckillSkuRelationAndSessionTo> collect = sms_seckill_sessions.stream().map((item) -> {
            SeckillSkuRelationAndSessionTo seckillSkuRelationAndSessionTo = new SeckillSkuRelationAndSessionTo();
            BeanUtils.copyProperties(item,seckillSkuRelationAndSessionTo);
          seckillSkuRelationAndSessionTo.setSmsSeckillSkuRelations(sms_seckill_sku_relation_mapper.selectList(new QueryWrapper<Sms_Seckill_Sku_Relation>().eq("promotion_session_id",item.getId())));
            return seckillSkuRelationAndSessionTo;
        }).collect(Collectors.toList());
        R r1=new R();
        r1.data(collect);
        return r1;
    }

    /*
     * @Description 或者当天的00:00:00
     * @param
     * @return java.lang.String
     * @author liusupeng
     * @date 2020/11/15 20:08
     */
    public String startDays(){

        LocalDate now=LocalDate.now();//当前时间
        LocalTime min=LocalTime.MIN;
        LocalDateTime start=LocalDateTime.of(now,min);
        return start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
    /*
     * @Description 或者从当天开始计算三天的时间的23:59:59
     * @param
     * @return java.lang.String
     * @author liusupeng
     * @date 2020/11/15 20:08
     */
    public String endDays() {
        LocalDate now = LocalDate.now();//当前时间
        LocalDate threeForDays = now.plusDays(2);//当前时间开始延后三天
        LocalTime max = LocalTime.MAX;
        LocalDateTime end = LocalDateTime.of(threeForDays, max);

        return end.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

    }
    /***
     * 后台查询每日秒杀活动
     * @param pages
     * @param sss
     * @return
     */
        @Override
       public LayuiUtils<Sms_Seckill_Session> find_admin_Mei_Ri_All(Page<Sms_Seckill_Session> pages, Sms_Seckill_Session sss){
        LayuiUtils<Sms_Seckill_Session> layuiUtils = new LayuiUtils<>();

        QueryWrapper<Sms_Seckill_Session> queryWrapper = new QueryWrapper<>();

//        queryWrapper.ge("start_time",new Date()).le("start_time",)

        queryWrapper.lambda().ge(Sms_Seckill_Session::getStart_time,new Date()).le(Sms_Seckill_Session::getStart_time,"DATE_ADD(date(now()),INTERVAL 1 DAY)");
        Page<Sms_Seckill_Session> sms_seckill_sessionPage = sms_seckill_session_mapper.selectPage(pages, queryWrapper);

        layuiUtils.setCount(sms_seckill_sessionPage.getTotal());
        layuiUtils.setData(sms_seckill_sessionPage.getRecords());

        return layuiUtils;
    }





        @Override
        public LayuiUtils<Sms_Seckill_Session> find_admin_Mei_Ri_All(Integer page, Integer limit, Sms_Seckill_Session sss) {
            LayuiUtils<Sms_Seckill_Session> layuiUtils = new LayuiUtils<>();

            List<Sms_Seckill_Session>  getAll = sms_seckill_session_mapper.find_admin_Mei_Ri_All(page,limit, sss);

            Long  getCount = sms_seckill_session_mapper.getCount(sss);
            layuiUtils.setCount(getCount);
            layuiUtils.setData(getAll);

            return layuiUtils;

        }


}
